﻿#region Imports
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using DirectoryAnalyzer.Enumerations;
#endregion

namespace DirectoryAnalyzer.Controls.Converters
{
    /// <summary>
    /// Provides iversion of provided value against data type
    /// </summary>
    public class InversionConverter : IValueConverter
    {
        #region Converter Methods
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            eConversionValueType type = (eConversionValueType)Enum.Parse(typeof(eConversionValueType), parameter.ToString()); ;

            // Validating values
            switch (type)
            {
                case eConversionValueType.Boolean:
                    try { bool source = (bool)value; }
                    catch (Exception) { return false; }
                    break;

                case eConversionValueType.Visibility:
                    try { Visibility source = (Visibility)value; }
                    catch (Exception) { return Visibility.Collapsed; }
                    break;

                case eConversionValueType.Number:
                    try { double source = (double)value; }
                    catch (Exception) { return 0; }
                    break;
            }

            // Conversion
            if (parameter != null)
            {
                switch (type)
                {
                    case eConversionValueType.Boolean:
                        return !(bool)value;
                        break;

                    case eConversionValueType.Visibility:
                        {
                            switch ((Visibility)value)
                            {
                                case Visibility.Visible:
                                    return false;
                                case Visibility.Collapsed:
                                case Visibility.Hidden:
                                default:
                                    return true;
                            }
                        }

                        break;

                    case eConversionValueType.Number:
                        if (value.GetType() == typeof(int))
                            return (int)value * -1;
                        else if (value.GetType() == typeof(double))
                            return (double)value * -1;
                        break;
                }
            }

            throw new InvalidOperationException();
        }

        /// <summary>
        /// ConvertBack to original value
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Convert(value, targetType, parameter, culture);
        }
        #endregion
    }
}
